#include "HeroMech.h"
#include "Mech.h"
#include "RandomMech.h"
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;
using namespace CocosDenshion;

HeroMech::HeroMech() : Mech()
{
}

bool HeroMech::initWithLayer(HelloWorld *layer,int kind,int hp)
{
	if( Mech::initWithLayer(layer,kind,hp) )
	{
		_layer = layer;
		this->schedule(schedule_selector(HeroMech::selectEnemy));
		return true;
	}

    return false;
}


void HeroMech::selectEnemy(float dt)
{
	float ATTACK_RANGE = 300;

	CCObject* it = NULL;
	
	RandomMech *target = NULL;
	float minDist = FLT_MAX; 
	CCARRAY_FOREACH(_layer->enemyTanks, it)
	{
		RandomMech *enemy = dynamic_cast<RandomMech *>(it);
		float dist = ccpDistance(this->getPosition(),enemy->getPosition());
		if( dist < ATTACK_RANGE && dist < minDist )
		{
			minDist = dist;
			target = enemy;
		}
	}

	if( target )
	{
		this->shooting = true;
		this->shootToward(target->getPosition());
	}
	else
	{
		this->shooting = false;
	}
}

